<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\stat;

use app\enum\AnalysisEnum;
use app\enum\CommonEnum;
use app\model\mongo\MongoStatModel;
use app\service\analysis\AnalysisService;
use app\service\analysis\stat\kuaishou\PicStatService as KS;
use app\service\analysis\stat\ocean\PicStatService as OE;
use app\service\analysis\stat\qq\PicStatService as QQ;
use app\service\BaseService;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

final class StatPicService extends BaseService
{
    private ?StatService $service;

    public function __construct(int $mediaId = 0)
    {
        if ($mediaId == CommonEnum::OE_MEDIA) {
            $this->service = new OE();
        }
        if ($mediaId == CommonEnum::KS_MEDIA) {
            $this->service = new KS();
        }
        if ($mediaId == CommonEnum::QQ_MEDIA) {
            $this->service = new QQ();
        }
    }

    /**
     * 是否是汇总服务
     */
    public function isService(): bool
    {
        return $this->service instanceof StatService || self::setError('获取日报服务失败');
    }

    /**
     * 账户汇总变更数据.
     * @param int $page 当前页
     * @param int $limit 分页数
     * @return array|Collection|MongoStatModel[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAccountAppendList(int $page = 1, int $limit = 200)
    {
        $model = AnalysisService::getStatModelList($this->service::getMediaId(), AnalysisEnum::TYPE_PIC, AnalysisEnum::STAT_ACCOUNT);
        if (! $model instanceof MongoStatModel) {
            return [];
        }
        return $model->where(['is_change' => MongoStatModel::IS_CHANGE_NO])->limit(($page - 1) * $limit, $limit)->select();
    }

    /**
     * 按用户和视频汇总.
     * @param array $where 查询参数
     * @return float[]
     */
    public function getUserStat(array $where = []): array
    {
        return $this->service->getClientStat($where);
    }

    /**
     * 账户汇总变更数据.
     * @param int $page 当前页
     * @param int $limit 分页数
     * @return array|Collection|MongoStatModel[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAccountChangeList(int $page = 1, int $limit = 200)
    {
        $model = AnalysisService::getStatModelList($this->service::getMediaId(), AnalysisEnum::TYPE_PIC, AnalysisEnum::STAT_ACCOUNT);
        if (! $model instanceof MongoStatModel) {
            return [];
        }
        return $model->where(['is_change' => MongoStatModel::IS_CHANGE_YES])->limit(($page - 1) * $limit, $limit)->select();
    }

    /**
     * 按客户和视频汇总.
     * @param array $where 查询参数
     * @return float[]
     */
    public function getClientStat(array $where = []): array
    {
        return $this->service->getClientStat($where);
    }

    /**
     * 客户汇总变更数据.
     * @param int $page 当前页
     * @param int $limit 分页数
     * @return array|Collection|MongoStatModel[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getClientChangeList(int $page = 1, int $limit = 200)
    {
        $model = AnalysisService::getStatModelList($this->service::getMediaId(), AnalysisEnum::TYPE_PIC, AnalysisEnum::STAT_CLIENT);
        if (! $model instanceof MongoStatModel) {
            return [];
        }
        return $model->where(['is_change' => MongoStatModel::IS_CHANGE_YES])->limit(($page - 1) * $limit, $limit)->select();
    }

    /**
     * 按视频汇总.
     * @param array $where 查询参数
     * @return float[]
     */
    public function getPicStat(array $where = []): array
    {
        return $this->service->getPicStat($where);
    }
}
